<?php

//STemplate::assign('audiopath',$config['tmpaudiopath']);
/* * ************************************************************************************************
  |
  | AudioShare - Audio Sharing Script
  | http://www.audiosharescript.com
  | webmaster@audiosharescript.com
  |
  |**************************************************************************************************
  |
  | By using this software you agree that you have read and acknowledged our End-User License
  | Agreement available at http://www.audiosharescript.com/eula.html and to be bound by it.
  |
  | Copyright (c) 2010 AudioShareScript.com. All rights reserved.
  |************************************************************************************************* */

include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

$mainmenu = "8";
STemplate::assign('mainmenu', $mainmenu);

//Get Variables
$getsortby = htmlentities(strip_tags($_REQUEST[sortby]));
$sortby = escape($getsortby);
$getfilter = htmlentities(strip_tags($_REQUEST[filter]));
$filter = escape($getfilter);
$getpage = htmlentities(strip_tags($_REQUEST[page]));
$page = escape($getpage);

$cat = "";
if (isset($_REQUEST["cat"]) && (int)$_REQUEST["cat"]>0 && $_REQUEST["cat"]!=""){
    $cat = intval($_REQUEST["cat"]);
}

$featured = "all";
$featureCond = "";
$featured7Cond = "";

if (isset($_GET["featured"]) && ($_GET["featured"] != ""))
    $featured = trim(stripcslashes($_GET["featured"]));



switch ($featured) {
    case "all":
        $featureCond = "";
        $featured7Cond = "";
        break;
    case "1":
        $featureCond = " and featured='1' ";
        $featured7Cond = " and A.featured='1' ";
        break;
    case "0":
        $featureCond = " and featured='0'";
        $featured7Cond = " and A.featured='0' ";
        break;
}
if ($cat != "") {
    $featureCond .= " and categories='" . $cat . "'";
    $featured7Cond .= " and A.categories='" . $cat . "' ";
}

STemplate::assign('featured', $featured);

if ($sortby == "a") {
    $sort = "asc";
} else {
    $sortby = "z";
    $sort = "desc";
}

if ($page == "") {
    $page = "1";
}
$currentpage = $page;

if ($page >= 2) {
    $pagingstart = ($page - 1) * $config[items_per_page];
} else {
    $pagingstart = "0";
}

if ($filter == "0") {
    $query1 = "SELECT count(*) as total from audios where public='1' AND active='1' $featureCond order by time_added $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios where public='1' AND active='1' $featureCond order by time_added $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[826]";
    $showseotitle = "$lang[174]";
    $submenu = "0";
} elseif ($filter == "1") {
    $query1 = "SELECT count(*) as total from audios where public='1' AND active='1' $featureCond order by viewcount $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios where public='1' AND active='1' $featureCond order by viewcount $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[827]";
    $showseotitle = "$lang[178]";
    $submenu = "1";
} elseif ($filter == "2") {
    $query1 = "SELECT count(*) as total from audios where public='1' AND active='1' $featureCond order by rating $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios where public='1' AND active='1' $featureCond order by rating $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[828]";
    $showseotitle = "$lang[179]";
    $submenu = "2";
} elseif ($filter == "3") {
    $query1 = "SELECT count(*) as total from audios where public='1' AND active='1' $featureCond order by commentcount $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios where public='1' AND active='1' $featureCond order by commentcount $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[829]";
    $showseotitle = "$lang[180]";
    $submenu = "3";
} elseif ($filter == "4") {
    $query1 = "SELECT count(*) as total from audios where public='1' AND active='1' $featureCond order by favoritecount $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios where public='1' AND active='1' $featureCond order by favoritecount $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[830]";
    $showseotitle = "$lang[182]";
    $submenu = "4";
} elseif ($filter == "5") {
    $query1 = "SELECT count(*) as total from audios where public='1' AND active='1' $featureCond order by runtime $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios where public='1' AND active='1' $featureCond order by runtime $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[795]";
    $showseotitle = "$lang[181]";
    $submenu = "5";
} elseif ($filter == "6") {
    $query1 = "SELECT count(*) as total from audios where public='1' AND active='1' AND featured='1' order by time_added $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios where public='1' AND active='1' AND featured='1' order by time_added $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[803]";
    $showseotitle = "$lang[53]";
    $submenu = "6";
} elseif ($filter == "7") {
    if ($_SESSION[USERID] == "") {
        header("Location:$config[baseurl]/login.php");
        exit;
    }

    $query1 = "SELECT count(*) as total from audios A, audios_favorited B where B.USERID='$_SESSION[USERID]' AND A.AUDIOID=B.AUDIOID AND A.public='1' AND A.active='1' $feature7Cond order by A.time_added $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios A, audios_favorited B where B.USERID='$_SESSION[USERID]' AND A.AUDIOID=B.AUDIOID AND A.public='1' AND A.active='1' $feature7Cond order by A.time_added $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[815]";
    $showseotitle = "$lang[831]";
    $submenu = "7";
} else {
    $query1 = "SELECT count(*) as total from audios where public='1' AND active='1' $featureCond order by time_added $sort limit $config[maximum_results]";
    $query2 = "SELECT * from audios where public='1' AND active='1' $featureCond order by time_added $sort limit $pagingstart, $config[items_per_page]";
    $showtitle = "$lang[826]";
    $showseotitle = "$lang[174]";
    $submenu = "0";
}

STemplate::assign('showtitle', $showtitle);
STemplate::assign('pagetitle', $showtitle);
STemplate::assign('showseotitle', $showseotitle);
STemplate::assign('filter', $filter);
STemplate::assign('submenu', $submenu);

$executequery1 = $conn->Execute($query1);

$totalaudios = $executequery1->fields['total'];
if ($totalaudios > 0) {

    if ($executequery1->fields['total'] <= $config[maximum_results]) {
        $total = $executequery1->fields['total'];
    } else {
        $total = $config[maximum_results];
    }

    $toppage = ceil($total / $config[items_per_page]);
    if ($toppage == 0) {
        $xpage = $toppage + 1;
    } else {
        $xpage = $toppage;
    }


    $executequery2 = $conn->Execute($query2);
    $users = $executequery2->getrows();
    $beginning = $pagingstart + 1;
    $ending = $pagingstart + $executequery2->recordcount();

    $pagelinks = "";
    $k = 1;
    $theprevpage = $currentpage - 1;
    $thenextpage = $currentpage + 1;

    $featureCatUrl = "&featured=$featured";
    if ($cat != "")
        $featureCatUrl .="&cat=$cat";

    if ($currentpage > 0) {
        ## paging starts here...

        $pagelinks.='<table width="100%" cellspacing="2" cellpadding="2" border="0">';
        $pagelinks.='<tr>';
        $pagelinks.='<td width="33%">';
        $pagelinks.='<a href="javascript:void(0);"><img height="16" width="16" border="0" align="absmiddle" src="' . $config[imageurl] . '/ico_rss_s.gif" alt="RSS"  /></a>';
        $pagelinks.='<span class="pag"> page ' . $currentpage . ' of ' . $total . '</span>';
        $pagelinks.='</td>';
        $pagelinks.='<td width="33%" align="right">';
        $pagelinks.="<div class=\"pagination\">";
        if ($currentpage > 1) {
            $pagelinks.="<a href='$thebaseurl/browse/audios/$sortby/$filter/1/$showseotitle$featureCatUrl'>$lang[611]</a>";
            $pagelinks.="<span class='ellipsis'>...</span>";
            $pagelinks.="<a href='$thebaseurl/browse/audios/$sortby/$filter/$theprevpage/$showseotitle$featureCatUrl'>&laquo; $lang[612]</a>";
        } else {
            $pagelinks.="<span>&lt;</span>";
        }
        $counter = 0;
        $lowercount = $currentpage - 5;
        if ($lowercount <= 0)
            $lowercount = 1;

        while ($lowercount < $currentpage) {
            $pagelinks.="<a href='$thebaseurl/browse/audios/$sortby/$filter/$lowercount/$showseotitle$featureCatUrl'>$lowercount</a>";
            $lowercount++;
            $counter++;
        }

        $pagelinks.="<span class='current'><span>$currentpage</span></span>";

        $uppercounter = $currentpage + 1;

        while (($uppercounter < $currentpage + 10 - $counter) && ($uppercounter <= $toppage)) {
            $pagelinks.="<a href='$thebaseurl/browse/audios/$sortby/$filter/$uppercounter/$showseotitle$featureCatUrl'>$uppercounter</a>";
            $uppercounter++;
        }

        if ($currentpage < $toppage) {
            $pagelinks.="<a href='$thebaseurl/browse/audios/$sortby/$filter/$thenextpage/$showseotitle$featureCatUrl>$lang[613] &raquo;</a>";
            $pagelinks.="<span class='ellipsis'>...</span>";
            $pagelinks.="<a href='$thebaseurl/browse/audios/$sortby/$filter/$toppage/$showseotitle$featureCatUrl'>$lang[614]</a>";
        } else {
            $pagelinks.="<span class=\"disable\">&gt;</span>";
        }
        $pagelinks.="</div>";
        $pagelinks.='</td>';
        $pagelinks.='</tr>';
        $pagelinks.="</table>";
        ## paging ends here
    }
} else {
    $error = "$lang[832]";
    STemplate::assign('error', $error);
}


//TEMPLATES BEGIN
STemplate::assign('beginning', $beginning);
STemplate::assign('ending', $ending);
STemplate::assign('pagelinks', $pagelinks);
STemplate::assign('total', $total);
STemplate::assign('listaudios', $users);
STemplate::assign('featureCatUrl', $featureCatUrl);
STemplate::assign('cat', $cat);
STemplate::display('header.tpl');
STemplate::display('audios.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>